<template>
  <!--  分配他人弹框  -->
  <a-modal
    :visible.sync="showAllocationModal"
    title="选择分配人员"
    :footer="null"
    @cancel="closeModal"
    :destroy-on-close="true"
    width="30vw"
  >
    <a-form :form="state.tableParams" layout="inline">
      <a-form-item label="分配人员：">
        <a-spin :spinning="state.allocationModalSpinning">
          <a-select
            v-model="state.assignedID"
            show-search
            option-filter-prop="children"
            placeholder="请选择"
            style="width: 15vw"
          >
            <a-select-option
              v-for="item in state.allocationOptions"
              :key="item.userId"
            >
              {{ item.fullName }}
            </a-select-option>
          </a-select>
        </a-spin>
      </a-form-item>
    </a-form>
    <div class="text-center mt20">
      <a-space>
        <a-button
          type="primary"
          @click="confirmAllocation"
          :loading="
            state.allocationModalSpinning || state.loadingConfirmAllocationBtn
          "
        >
          确定
        </a-button>
        <a-button @click="closeModal"> 取消 </a-button>
      </a-space>
    </div>
  </a-modal>
</template>
<script>
import {
  todo_get_config_user_post,
  todo_handover_post
} from 'server/modules/7537'
export default {
  data() {
    return {
      state: {
        tableParams: null,
        allocationModalSpinning: false,
        assignedID: null,
        allocationOptions: null,
        loadingConfirmAllocationBtn: false
      }
    }
  },
  props: ['showAllocationModal', 'record'],
  watch: {
    showAllocationModal(val) {
      if (val) {
        this.state.allocationModalSpinning = true
        // TODO 根据  业务代码 + 类型（如：核价/调整工艺，确定由哪个方法实际执行）
        this.todoGetConfigUser(this.record)
        // TODO 初始化弹窗select框默认选中人
      }
    }
  },
  methods: {
    // 获取转移人员数组
    async todoGetConfigUser(task) {
      const params = {}
      // 核价员时
      if (task.businessType === 2) {
        params.businessModelCode = task.businessModelCode
        params.userDuty = 'valuer'
      }

      const { success, retInfo, data } = await todo_get_config_user_post(params)
      this.state.allocationModalSpinning = false
      if (success) {
        this.state.allocationOptions = data
        for (const item of data) {
          if (task.receiver === item.userId) {
            this.state.assignedID = item.userId
            break
          }
        }
      } else {
        console.log(retInfo)
      }
    },
    async confirmAllocation() {
      this.state.loadingConfirmAllocationBtn = true

      // // TODO 調用分配接口
      const { success, retInfo, data } = await todo_handover_post({
        id: this.record.id,
        receiver: this.state.assignedID
      })
      this.state.loadingConfirmAllocationBtn = false
      if (success) {
        this.$message.success(data)
        this.$emit('refresh')
      } else {
        console.log(retInfo)
      }
      this.closeModal()
    },
    closeModal() {
      this.$emit('closeModal')
    }
  }
}
</script>
<style lang="less" scoped></style>
